package ace.module.product.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.utils.SaveContextUtils;
import ace.module.common.core.impl.manager.impl.AbstractEntityOfAppManager;
import ace.module.product.api.enums.PmsCommonVideoObjectTypeEnum;
import ace.module.product.api.model.dto.PmsCommonVideoDto;
import ace.module.product.core.impl.dao.entity.PmsCommonVideo;
import ace.module.product.core.impl.dao.entity.PmsProduct;
import ace.module.product.core.impl.manager.PmsCommonVideoManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 商品通用视频 spring data jdbc querydsl rsql manager implement.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class PmsCommonVideoManagerImpl
    extends AbstractEntityOfAppManager<PmsCommonVideo, Long, PmsCommonVideoDto>
    implements PmsCommonVideoManager {

  @Override
  public SaveContext<PmsCommonVideo> assembleProductMainVideoSaveContext(PmsProduct pmsProduct, String videoUrl) {
    if (StringUtils.isEmpty(videoUrl)) {
      return SaveContextUtils.empty();
    }
    PmsCommonVideo pmsCommonVideo = PmsCommonVideo.builder()
        .id(this.getNewId())
        .pmsAppId(pmsProduct.getPmsAppId())
        .objectId(pmsProduct.getId())
        .objectParentId(pmsProduct.getPmsAppId())
        .objectType(PmsCommonVideoObjectTypeEnum.PRODUCT.getCode())
        .sort(CoreConstant.DEFAULT_SORT)
        .video(videoUrl)

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(System.currentTimeMillis())
        .updateTime(System.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    return SaveContextUtils.ofNew(pmsCommonVideo);
  }
}
